package com.project.rfidpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.rfidpage.aop.TagLog;
import com.project.rfidpage.dto.RidTidMaintenanceParam;
import com.project.rfidpage.entity.TidMaintenance;
import com.project.rfidpage.entity.TidMaintenanceRecord;
import com.project.rfidpage.service.TidMaintenanceService;
import com.project.rfidpage.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/api/rfidpage/tid/maintenance")
public class TidMaintenanceController extends BaseController {

    @Resource
    TidMaintenanceService tidMaintenanceService;

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询电子标签维护任务列表")
    public RestResult page(@RequestBody RidTidMaintenanceParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(tidMaintenanceService.getListForPage(param)));
    }

    /**
     * 详细查询
     */
    @PostMapping("detail")
    @TagLog(operation = 5, content = "查询电子标签维护记录详细列表")
    public RestResult detail(@RequestBody RidTidMaintenanceParam param) {
        this.getPage(false, param);
        return RestResult.ok(tidMaintenanceService.getRecordListForPage(param));
    }

    /**
     * 新增基站维护任务
     */
    @PutMapping("add")
    @TagLog(operation = 2, content = "新增电子标签维护任务")
    public RestResult add(@RequestHeader(value = "user") String user, @RequestBody TidMaintenance param) {
        try {
            param.setTask_creator(user);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate now = LocalDate.now();
            String nowStr = formatter.format(now);
            param.setTask_create_date(Integer.parseInt(nowStr));
            param.setTask_status(0);
            tidMaintenanceService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 新增基站维护记录
     */
    @PutMapping("record/add")
    @TagLog(operation = 2, content = "新增电子标签维护记录")
    public RestResult add(@RequestHeader(value = "user") String user, @RequestBody TidMaintenanceRecord param) {
        List<TidMaintenance> list = tidMaintenanceService.getById(param.getTask_id());
        if (list.size() == 0) {
            return RestResult.error("该条任务不存在");
        } else {
            try {
                param.setTask_recipient(user);
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
                LocalDate now = LocalDate.now();
                String nowStr = formatter.format(now);
                param.setTask_execution_date(Integer.parseInt(nowStr));
                tidMaintenanceService.insertRecord(param);
                list.get(0).setTask_status(param.getTask_status());
                list.get(0).setTask_id(null);
                tidMaintenanceService.update(list.get(0), param.getTask_id());
                return RestResult.ok();
            } catch (Exception e) {
                return RestResult.error(e.getMessage());
            }
        }
    }
}
